package com.ahdy.service.impl;

import com.ahdy.entity.ElderlyFamilyRelation;
import com.ahdy.mapper.ElderlyFamilyRelationMapper;
import com.ahdy.service.ElderlyFamilyRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 老人与子女关联关系服务实现类
 */
@Service
public class ElderlyFamilyRelationServiceImpl implements ElderlyFamilyRelationService {

    @Autowired
    private ElderlyFamilyRelationMapper elderlyFamilyRelationMapper;

    @Override
    @Transactional
    public Integer addRelation(ElderlyFamilyRelation relation) {
        elderlyFamilyRelationMapper.insert(relation);
        return relation.getId();
    }

    @Override
    @Transactional
    public int updateRelation(ElderlyFamilyRelation relation) {
        return elderlyFamilyRelationMapper.update(relation);
    }

    @Override
    @Transactional
    public int deleteRelation(Integer id) {
        return elderlyFamilyRelationMapper.deleteById(id);
    }

    @Override
    @Transactional
    public int deleteRelationByElderlyAndFamily(Integer elderlyId, Integer familyId) {
        return elderlyFamilyRelationMapper.deleteByElderlyAndFamily(elderlyId, familyId);
    }

    @Override
    public ElderlyFamilyRelation getRelationById(Integer id) {
        return elderlyFamilyRelationMapper.findById(id);
    }

    @Override
    public List<ElderlyFamilyRelation> getRelationsByElderlyId(Integer elderlyId) {
        return elderlyFamilyRelationMapper.findByElderlyId(elderlyId);
    }

    @Override
    public List<ElderlyFamilyRelation> getRelationsByFamilyId(Integer familyId) {
        return elderlyFamilyRelationMapper.findByFamilyId(familyId);
    }

    @Override
    public ElderlyFamilyRelation getRelationByElderlyAndFamily(Integer elderlyId, Integer familyId) {
        return elderlyFamilyRelationMapper.findByElderlyAndFamily(elderlyId, familyId);
    }

    @Override
    public boolean checkRelationExists(Integer elderlyId, Integer familyId) {
        return elderlyFamilyRelationMapper.countByElderlyAndFamily(elderlyId, familyId) > 0;
    }
} 